package com.service;

import com.domain.CardBatchRechDetails;
import com.domain.CardTrans;
import com.domain.RechSubsidy;
import com.mapper.second.CardBatchRechDetailsMapper;
import com.mapper.second.CardTransMapper;
import com.service.seinterface.CardBatchRechDetailsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service("cardBatchRechDetailsService")
public class CardBatchRechDetailsServiceImpl implements CardBatchRechDetailsService {

    @Resource
    CardBatchRechDetailsMapper cardBatchRechDetailsMapper;

    @Resource
    CardTransMapper cardTransMapper;

    @Override
    public List<RechSubsidy> selectByCardNo(String no,String card) {
        List<CardBatchRechDetails> cardBatchRechDetails=cardBatchRechDetailsMapper.selectByCardNo(no);
        List<CardTrans> cardTrans=cardTransMapper.selectAllByCard(card);
        List<RechSubsidy> rechSubsidies=new ArrayList<>();
        RechSubsidy rechSubsidy=null;
        for(CardBatchRechDetails cardBatchRechDetails1:cardBatchRechDetails){
            rechSubsidy=new RechSubsidy();
            rechSubsidy.setType(1131);
            rechSubsidy.setTime(cardBatchRechDetails1.getLastRechdate());
            rechSubsidy.setAmt(cardBatchRechDetails1.getAmt());
            rechSubsidies.add(rechSubsidy);
        }

        for(CardTrans cardTrans1:cardTrans){
            rechSubsidy=new RechSubsidy();
            rechSubsidy.setType(3);
            rechSubsidy.setTime(cardTrans1.getCreateDate());
            rechSubsidy.setAmt(cardTrans1.getAmt());
            rechSubsidies.add(rechSubsidy);
        }

        Collections.sort(rechSubsidies,new Comparator<RechSubsidy>() {

            @Override
            public int compare(RechSubsidy o1, RechSubsidy o2) {
                // TODO Auto-generated method stub
                if(o1.getTime().getTime()>o2.getTime().getTime()){
                    return -1;
                }else if(o1.getTime().getTime()<o2.getTime().getTime()){
                    return 1;
                }else{
                    return 0;
                }

            }
        });

        return rechSubsidies;
    }
}
